#pragma once
#include "Color.h"
#include "Viewport.h"
#include <iostream>
#include "math.h"
#include <string.h>
#include "../domain/Utils.h"
#include "../domain/Graph.h"
#include "../domain/Map.h"
#include <sstream>

#define window_width  800
#define window_height 600
#define X 0
#define Y 1

class GraphDrawer {
private:
	SDL_Surface *screen;
	SDL_Surface *avenue;
	SDL_Surface *avenueV;
	SDL_Surface *street;
	SDL_Surface *streetV;
	SDL_Surface *highway;
	SDL_Surface *highwayV;
	SDL_Surface *street23;
	SDL_Surface *crossroads;
	SDL_Surface *car;
	SDL_Surface *grass;

	SDL_Surface *hotel;
	SDL_Surface *restaurant;
	SDL_Surface *comisaria;
	SDL_Surface *escuela;
	SDL_Surface *hospital;
	SDL_Surface *originDot;
	SDL_Surface *destinyDot;

	static int debug;
	float yMin;
	float xMin;
	float xMax;
	float yMax;


	float calcLength(int x1, int y1, int x2, int y2);

	float calcAngle(int x1, int y1, int x2, int y2, int inverse);

	float calcLength(SDL_Surface* surface, float angle);

	SDL_Surface* getSurfaceFor(float angle, SDL_Surface* sprite, int inverse);

	SDL_Surface* getSurfaceFor(string type);

	float getFactor(float a, float b, int c, int d);

	float calcXYRatio(SDL_Surface* surface);

	int calcWidth(float length, float xyRatio);

	void calcNextXY(int i, int* x, int*y, int spriteW, int spriteH, float tanAngle, float angle, float XYRatio, int inverse);

	int findXY(int x1, int y1, int x2, int y2, int* xo, int* xf, int* yo, int* yf);

	void drawEdge(Edge* e, Vertex* origin, Vertex* destination);

	void drawVertexesAndEdges(Graph* g);

	void drawEdges(Graph* g, Vertex *origin);

	void drawObjects(Map* map);
	
	void rotate(int x, int y, float cosAngle, float sinAngle, int* newX, int* newY);

	int calcNewSize(int w, int h, float cosAngle, float sinAngle, int* newW, int* newH, int* maxYRes);

	int getRealX(float x);

	int getRealY(float y);

	void searchMaxAndMin(Graph *graph);

	void calcObjectPos(float& x, float& y,float x0, float xf, float y0, float yf, float factorX, float factorY, int init, int last, int number, bool esPar);

public:

	GraphDrawer();

	virtual ~GraphDrawer();

	static int makeTransparent(SDL_Surface* surface, Color c);

	static SDL_Surface * loadBMP(const char* fileName);

	int initImages();

	static void drawIMG(SDL_Surface *img, int x, int y, SDL_Surface *background);

	static void drawIMG(SDL_Surface *src, int xSrc, int ySrc, int wSrc, int hSrc, int xDest, int yDest, SDL_Surface *dest);

	void drawPixel(SDL_Surface *screen, int x, int y, Uint32 color);

	void drawPixel(SDL_Surface *screen, int x, int y, Color c);

	void drawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B);

	void lock(SDL_Surface *screen);

	void unlock(SDL_Surface *screen);

	void fillSurface(SDL_Surface *screen, Color c);

	void fillSurfaceWithTexture(SDL_Surface* screen, SDL_Surface* texture);

	SDL_Surface* getTextureFor(Edge* e, float angle);

	void drawHLine(SDL_Surface *screen, int leftmost, int rightmost, int y, Color c);

	static void drawSpriteV(SDL_Surface* sprite, int x, int y, int w, int h, SDL_Surface* backgroud);

	static void drawSpriteH(SDL_Surface* sprite, int x, int y, int w, int h, SDL_Surface* backgroud);

	void drawSprite(SDL_Surface* sprite, int xo, int yo, int xf, int yf, SDL_Surface* backgroud);

	SDL_Surface* drawGraph(Map* map);

	void drawLine(SDL_Surface *screen,  float xo, float yo, float xf, float yf );

	float getXMin() const;

	float getYMin() const;

	float getXMax()const;

	float getYMax()const;

	float fixXPosition(float point);

	float fixYPosition(float point);

	SDL_Surface* getOriginDot();

	SDL_Surface* getDestinyDot();

};
